package com.fyyg.service.jms;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.fyyg.common.sms.yimei.YiMeiSendMessage;
import com.fyyg.common.util.date.YXDate;
import com.fyyg.common.wechat.WeChatSendMessage;

public class ConsumerMessageListener implements MessageListener {

    private final static Logger logger = LogManager.getLogger(ConsumerMessageListener.class);

	@Override
	public void onMessage(Message message) {
		 TextMessage textMessage = (TextMessage) message;
        try {
            logger.info("----------------------------------------触发消息队列\t"+message);
            
    		try {
    			InetAddress inetAddress = InetAddress.getLocalHost();
    			System.out.println("----------------------------------------"+inetAddress.getHostAddress());
    			/**  运行环境判断 为哪台服务器 **/
    			if(inetAddress.getHostAddress().equals("192.168.1.55")){ 
    				 logger.info("----------------------------------------测试环境不发消息");
    				return;
    	        }
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		}
            
            messageQueuManage(textMessage.getText());
            
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
        
	}
	
	
	public  void messageQueuManage(String message){
		new JSONObject();
		JSONObject json = JSONObject.parseObject(message);
		String type = json.getString("type");
		switch (type) {
			case "SMS":	//发送短信
				String phone = json.getString("phone");
				String sednMessage = json.getString("message");
				String sendTime = json.getString("sendTime");
				if(YXDate.isValidDate(sendTime)){
					YiMeiSendMessage.timingMessage(phone, sednMessage,sendTime);
				}else{
					YiMeiSendMessage.sendMessage(phone, sednMessage);
				}
				break;
			case "WX":
				String wxtype=json.getString("wxtype");
				if(wxtype.equals("1")){//发送单图文消息
					String str_wxnos=json.getString("wxnos");
					String title=json.getString("title");
					String content=json.getString("content");
					String picUrl=json.getString("picUrl");
					String postUrl=json.getString("postUrl");
					String access_token=json.getString("access_token");
					WeChatSendMessage.sendNew(str_wxnos,title, content, picUrl, postUrl,access_token);
				}else if(wxtype.equals("2")){
					
				}
				break;
//			case "APP":
//				String uid = json.getString("uid");
//				String messge = json.getString("message");
//				String groupIds = json.getString("groupId");
//				PushExample.sendPush(uid, messge,groupIds);
//				JGPushExample.sendPush(uid, messge,groupIds);
//				break;
//			case "TK":	//退款    中退自动 其他的不自动
//				String groupId = json.getString("groupId");
//				if(StringUtil.isNotEmpty(groupId)){
//					TBusiShopProductBatchAdditionalGroup group = shopProductBatchAdditionalGroupService.selectByPrimaryKey(Integer.parseInt(groupId));
//					if(null!=group && group.getActivityId()==105){
//						systemRefundmentCoreService.activityRefundment(groupId);
//					}
//				}
//				break;
//			case "CORETK":	//核心退款，包括普通模式  未设置模式 组团模式  特价疯抢模式
//				String groupid = json.getString("groupId");
//				systemRefundmentCoreService.execute(groupid);
//				break;
		}
	}
	

}
